var express = require("express");
var router = express.Router();
var { CustomerModel } = require("../db/db");
var Objectid = require("mongodb").ObjectId;

router.get("/customer", async function (req, res) {
  var data = await CustomerModel.find({});
  res.send({ data: data });
});




router.delete("/customer", async function (req, res) {
  data = req.body;
  var id = Objectid(data["_id"]);
  await CustomerModel.find({ _id: id }).update({ $set: data });
  res.send({ code: 200 });
});

router.post("/customer", async function (req, res) {
  var id = Objectid(req.query.id);
  await CustomerModel.findByIdAndDelete({ _id: id }, function (err, data) {
    if (!err) {
      res.json({ message: "删除成功", code: 201 });
    }
  });
});

module.exports = router;
